{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f68ce4af",
   "metadata": {},
   "source": [
    "# OpenAI Agents Guardrails Demonstration\n",
    "\n",
    "This notebook demonstrates guardrails using the Agents SDK and how one can observe them using the AgentOps platform."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10bcf29b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install required packages\n",
    "%pip install -q agentops\n",
    "%pip install -q openai-agents\n",
    "%pip install -q dotenv pydantic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3be4e68",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import dependencies\n",
    "from pydantic import BaseModel\n",
    "from agents import (\n",
    "    Agent,\n",
    "    GuardrailFunctionOutput,\n",
    "    InputGuardrailTripwireTriggered,\n",
    "    RunContextWrapper,\n",
    "    Runner,\n",
    "    TResponseInputItem,\n",
    "    input_guardrail,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d0dddb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load API keys\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_api_key_here\")\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"your_openai_api_key_here\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "114e216b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize agentops and import the guardrail decorator\n",
    "import agentops\n",
    "from agentops import guardrail\n",
    "\n",
    "agentops.init(api_key=os.environ[\"AGENTOPS_API_KEY\"], tags=[\"agentops-example\"], auto_start_session=False)\n",
    "tracer = agentops.start_trace(trace_name=\"OpenAI Agents Guardrail Example\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bf8b54d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# OpenAI Agents SDK guardrail example with agentops guardrails decorator for observability\n",
    "class MathHomeworkOutput(BaseModel):\n",
    "    is_math_homework: bool\n",
    "    reasoning: str\n",
    "\n",
    "\n",
    "guardrail_agent = Agent(\n",
    "    name=\"Guardrail check\",\n",
    "    instructions=\"Check if the user is asking you to do their math homework.\",\n",
    "    output_type=MathHomeworkOutput,\n",
    ")\n",
    "\n",
    "\n",
    "@input_guardrail\n",
    "@guardrail(spec=\"input\")  # Specify guardrail type as input or output\n",
    "async def math_guardrail(\n",
    "    ctx: RunContextWrapper[None], agent: Agent, input: str | list[TResponseInputItem]\n",
    ") -> GuardrailFunctionOutput:\n",
    "    result = await Runner.run(guardrail_agent, input, context=ctx.context)\n",
    "\n",
    "    return GuardrailFunctionOutput(\n",
    "        output_info=result.final_output,\n",
    "        tripwire_triggered=result.final_output.is_math_homework,\n",
    "    )\n",
    "\n",
    "\n",
    "agent = Agent(\n",
    "    name=\"Customer support agent\",\n",
    "    instructions=\"You are a customer support agent. You help customers with their questions.\",\n",
    "    input_guardrails=[math_guardrail],\n",
    ")\n",
    "\n",
    "\n",
    "async def main():\n",
    "    # This should trip the guardrail\n",
    "    try:\n",
    "        await Runner.run(agent, \"Hello, can you help me solve for x: 2x + 3 = 11?\")\n",
    "        print(\"Guardrail didn't trip - this is unexpected\")\n",
    "\n",
    "    except InputGuardrailTripwireTriggered:\n",
    "        print(\"Math homework guardrail tripped\")\n",
    "\n",
    "\n",
    "await main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63bf8e09",
   "metadata": {},
   "outputs": [],
   "source": [
    "agentops.end_trace(tracer, end_state=\"Success\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "agentops (3.11.11)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
